package com.tsingteng.cosfun.callback;


import android.content.Intent;

import com.tsingteng.cosfun.MainActivity;
import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.bean.BaseDataBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.helper.ActivityManagers;
import com.tsingteng.cosfun.mvp.presenter.BasePresenter;
import com.tsingteng.cosfun.mvp.view.IView;
import com.tsingteng.cosfun.ui.login.LoginActivity;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;

/**
 * 描述: 通用observer回调
 * 创建人: 肖利
 * 时间: 2018/7/30
 * qq: (872179966)
 */

public abstract class RxObserver<T> extends BaseRxObserver<T> {
    private IView mView;

    public RxObserver(BasePresenter basePresenter) {
        super(basePresenter);
        mView = basePresenter.getView();
    }

    @Override
    public void onNext(BaseDataBean<T> baseDataBean) {
        if (baseDataBean.getStatus() == Config.REQUEST_CODE_SUCCESS) {
            onSuccess(baseDataBean.getData());
        } else if (baseDataBean.getStatus() == Config.REQUEST_FREEZE  ) {
            //冻结账号,退出登录
            ActivityManagers.getInstance().finishAllActivity();
//            ActivityManagers.getInstance().deleteFoundActivity();
//            ActivityManagers.getInstance().delShootActivity();
            LogonUtils.outLogon();
            Intent intent = new Intent(AppContext.getContext(), LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppContext.getContext().startActivity(intent);
        } else if (baseDataBean.getStatus()  == Config.LOGON_PAST) {
            AppUtils.showHintDialogToast(AppContext.getContext(),"登录失效,请重新登录!");
            LogonUtils.outLogon();
            Navigate.startMain(AppContext.getContext(),true);
            ActivityManagers.getInstance().finishAllActivity();
            LogonUtils.outLogon();
            if (AppUtils.isToLogonActivity()) {
                Intent intent = new Intent(AppContext.getContext(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                AppContext.getContext().startActivity(intent);
            }
        }else {
            onFaile(baseDataBean.getStatus(), baseDataBean.getMsg());
            mView.showError();
        }

    }

    protected abstract void onFaile(int errorCode, String errorMsg);

    protected abstract void onSuccess(T t);


}
